package com.baizhi.service.impl;

import com.baizhi.entity.User;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.UserService;
import com.baizhi.vo.PhoneCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author 2107class
 * @since 2021-12-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, Object> login(HttpServletRequest request, PhoneCode phoneCode) {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone",phoneCode.getPhone());
        User user1 = userMapper.selectOne(wrapper);
//        System.out.println("user1 = " + user1);
        String token = request.getSession(true).getId();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        if(user1==null){
            User user2 = new User();
            user2.setName(phoneCode.getPhone());
            user2.setPhone(phoneCode.getPhone());
            user2.setIntro("你好，哈哈哈哈哈哈");
            user2.setAvatar("https://p.qqan.com/up/2020-7/2020071308424438852.jpg"); //头   像路径
            userMapper.insert(user2); //验证码存于redis中  发送的验证码和用户填写的验证码比较 相等进行注册 不相等返回验证码错误
            if (valueOperations.get(phoneCode.getPhone()).equals(phoneCode.getCaptcha())){
                map.put("token",token);
                redisTemplate.opsForValue().set(token,user2);
            }else{
                throw new RuntimeException("验证码错误！");
            }
            return map;
        }else{
            if (valueOperations.get(phoneCode.getPhone()).equals(phoneCode.getCaptcha())){
                //userMapper.insert(user1); //验证码存于redis中  发送的验证码和用户填写的验证码比较 相等进行注册 不相等返回验证码错误
                map.put("token",token);
                redisTemplate.opsForValue().set(token,user1);
            }else{
                throw new RuntimeException("验证码错误！");
            }
            return map;
        }
    }

    //查询已登录用户信息
    /*@Override
    public User queryLogin(String token) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        User user = (User) valueOperations.get(token);
        return user;
    }*/

    //查询一个
    @Override
    public User queryOne(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public User update(User user) {
        userMapper.updateById(user);
        return user;
    }


}
